<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible" width="50%">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="数据日期" prop="refDate">
        <el-date-picker
          v-model="formData.refDate"
          :readonly="formType == 'detail'"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择"
        />
      </el-form-item>
      <el-form-item v-if="formData.dataTypeCode == 4 || formData.dataTypeCode == 6" label="小时" prop="refHour">
        <el-select
          :disabled="formType == 'detail'"
          v-model="formData.refHour"
          clearable
          placeholder="请选择"
        >
          <el-option v-for="item in refHourOptions" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item v-if="formData.dataTypeCode == 1" label="msgid" prop="msgid">
        <el-input :readonly="formType == 'detail'" v-model="formData.msgid" placeholder="请输入" />
      </el-form-item>
      <el-form-item v-if="formData.dataTypeCode == 1" label="标题" prop="title">
        <el-input :readonly="formType == 'detail'" v-model="formData.title" placeholder="请输入" />
      </el-form-item>
      <el-form-item v-if="formData.dataTypeCode != 5 && formData.dataTypeCode != 6" label="图文页（点击群发图文卡片进入的页面）阅读人数" prop="intPageReadUser">
        <el-input :readonly="formType == 'detail'" v-model="formData.intPageReadUser" placeholder="请输入" />
      </el-form-item>
      <el-form-item v-if="formData.dataTypeCode != 5 && formData.dataTypeCode != 6" label="图文页阅读次数" prop="intPageReadCount">
        <el-input :readonly="formType == 'detail'" v-model="formData.intPageReadCount" placeholder="请输入" />
      </el-form-item>
      <el-form-item v-if="formData.dataTypeCode != 5 && formData.dataTypeCode != 6" label="原文页（点击图文页“阅读原文”进入的页面）阅读人数，无原文页时此处数据为0" prop="oriPageReadUser">
        <el-input :readonly="formType == 'detail'" v-model="formData.oriPageReadUser" placeholder="请输入" />
      </el-form-item>
      <el-form-item v-if="formData.dataTypeCode != 5 && formData.dataTypeCode != 6" label="原文页阅读次数" prop="oriPageReadCount">
        <el-input :readonly="formType == 'detail'" v-model="formData.oriPageReadCount" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="分享人数" prop="shareUser">
        <el-input :readonly="formType == 'detail'" v-model="formData.shareUser" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="分享次数" prop="shareCount">
        <el-input :readonly="formType == 'detail'" v-model="formData.shareCount" placeholder="请输入" />
      </el-form-item>
      <el-form-item v-if="formData.dataTypeCode != 5 && formData.dataTypeCode != 6" label="收藏人数" prop="addToFavUser">
        <el-input :readonly="formType == 'detail'" v-model="formData.addToFavUser" placeholder="请输入" />
      </el-form-item>
      <el-form-item v-if="formData.dataTypeCode != 5 && formData.dataTypeCode != 6" label="收藏次数" prop="addToFavCount">
        <el-input :readonly="formType == 'detail'" v-model="formData.addToFavCount" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="数据类型" prop="dataTypeCode">
        <el-select
          :disabled="formType == 'detail'"
          v-model="formData.dataTypeCode"
          clearable
          placeholder="请选择"
          @change="dataTypeChange"
        >
          <el-option v-for="item in dataTypeOptions" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
    </el-form>
    <!-- 子表的表单 -->
    <el-tabs v-model="subTabsName">
      <el-tab-pane label="文章分析子表" name="weixinArticleAnalysisSource">
        <WeixinArticleAnalysisSourceForm ref="weixinArticleAnalysisSourceFormRef" :analysis-id="formData.id" :form-type="formType" :main-data="formData" />
      </el-tab-pane>
    </el-tabs>
    <template #footer>
      <el-button v-if="formType != 'detail'" @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { WeixinArticleAnalysisApi, WeixinArticleAnalysisVO } from '@/api/integration/weixinarticleanalysis'
import WeixinArticleAnalysisSourceForm from './components/WeixinArticleAnalysisSourceForm.vue'

/** 微信文章分析 表单 */
defineOptions({ name: 'WeixinArticleAnalysisForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  refDate: undefined,
  refHour: undefined,
  msgid: undefined,
  title: undefined,
  intPageReadUser: undefined,
  intPageReadCount: undefined,
  oriPageReadUser: undefined,
  oriPageReadCount: undefined,
  shareUser: undefined,
  shareCount: undefined,
  addToFavUser: undefined,
  addToFavCount: undefined,
  dataTypeCode: undefined,
  dataType: undefined
})
const formRules = reactive({
})
const formRef = ref() // 表单 Ref

/** 子表的表单 */
const subTabsName = ref('weixinArticleAnalysisSource')
const weixinArticleAnalysisSourceFormRef = ref()

const refHourOptions = [
  {
    value: 0,
    label: '1时',
  },
  {
    value: 100,
    label: '2时',
  },
  {
    value: 200,
    label: '3时',
  },
  {
    value: 300,
    label: '4时',
  },
  {
    value: 400,
    label: '5时',
  },
  {
    value: 500,
    label: '6时',
  },
  {
    value: 600,
    label: '7时',
  },
  {
    value: 700,
    label: '8时',
  },
  {
    value: 800,
    label: '9时',
  },
  {
    value: 900,
    label: '10时',
  },
  {
    value: 1000,
    label: '11时',
  },
  {
    value: 1100,
    label: '12时',
  },
  {
    value: 1200,
    label: '13时',
  },
  {
    value: 1300,
    label: '14时',
  },
  {
    value: 1400,
    label: '15时',
  },
  {
    value: 1500,
    label: '16时',
  },
  {
    value: 1600,
    label: '17时',
  },
  {
    value: 1700,
    label: '18时',
  },
  {
    value: 1800,
    label: '19时',
  },
  {
    value: 1900,
    label: '20时',
  },
  {
    value: 2000,
    label: '21时',
  },
  {
    value: 2100,
    label: '22时',
  },
  {
    value: 2200,
    label: '23时',
  },
  {
    value: 2300,
    label: '24时',
  }
]

const dataTypeOptions = [
  {
    value: 1,
    label: '图文群发每日数据',
  },
  {
    value: 3,
    label: '图文统计数据',
  },
  {
    value: 4,
    label: '图文统计分时数据',
  },
  {
    value: 5,
    label: '图文分享转发数据',
  },
  {
    value: 6,
    label: '图文分享转发分时数据',
  }
]

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await WeixinArticleAnalysisApi.getWeixinArticleAnalysis(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/* 数据类型 */
const dataTypeChange = (val: any) => {
  formData.value.dataType = dataTypeOptions.find((item) => item.value == val)?.label;
}

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 校验子表单
  try {
    await weixinArticleAnalysisSourceFormRef.value.validate()
  } catch (e) {
    subTabsName.value = 'weixinArticleAnalysisSource'
    return
  }
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as WeixinArticleAnalysisVO
    // 拼接子表的数据
    data.weixinArticleAnalysisSources = weixinArticleAnalysisSourceFormRef.value.getData()
    if (formType.value === 'create') {
      await WeixinArticleAnalysisApi.createWeixinArticleAnalysis(data)
      message.success(t('common.createSuccess'))
    } else {
      await WeixinArticleAnalysisApi.updateWeixinArticleAnalysis(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    refDate: undefined,
    refHour: undefined,
    msgid: undefined,
    title: undefined,
    intPageReadUser: undefined,
    intPageReadCount: undefined,
    oriPageReadUser: undefined,
    oriPageReadCount: undefined,
    shareUser: undefined,
    shareCount: undefined,
    addToFavUser: undefined,
    addToFavCount: undefined,
    dataTypeCode: undefined,
    dataType: undefined
  }
  formRef.value?.resetFields()
}
</script>
